package com.example.meetsmart.repository;

import com.example.meetsmart.model.Booking;
import com.example.meetsmart.model.Booking.BookingStatus;
import com.example.meetsmart.model.Room;
import com.example.meetsmart.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface BookingRepository extends JpaRepository<Booking, Long> {
    List<Booking> findByUser(User user);
    List<Booking> findByRoom(Room room);
    List<Booking> findByStatus(BookingStatus status);

    @Query("SELECT b FROM Booking b WHERE " +
            "b.room = :room AND " +
            "b.status = 'APPROVED' AND " +
            "((b.startTime <= :endTime AND b.endTime >= :startTime))")
    List<Booking> findConflictingBookings(
            @Param("room") Room room,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    @Query("SELECT b FROM Booking b WHERE " +
            "b.startTime >= :start AND b.startTime <= :end OR " +
            "b.endTime >= :start AND b.endTime <= :end")
    List<Booking> findBookingsBetween(
            @Param("start") LocalDateTime start,
            @Param("end") LocalDateTime end);

    @Query("SELECT b FROM Booking b WHERE " +
            "b.status = 'APPROVED' AND " +
            "b.startTime <= :now AND b.endTime >= :now")
    List<Booking> findCurrentBookings(@Param("now") LocalDateTime now);
}